#!/usr/bin/python
# Author: Anirudh Sanjeev (anirudh (@t) anirudhsanjeev (d0t) org)
"""
This file reads the diagnostics.list file, and converts it into categories and diagnostics that is in an unified format and can be passed between modules.
"""
from diagnostic import Diagnostic # to create diagnostics

from external.configobj import ConfigObj
"""

# This write a config file. For debugging, learning and unit testing only.
myoptions = {
        'stringify':True,
        'indent_type':'\t',

        }
config = ConfigObj(options = myoptions)
config.filename = "diagtest.list"

config['Network'] = {}
config["Network"]["ifconfig"] = {
        'type':'command',
        'command':'ifconfig',
        'title':'Network details',
        'description':'Description goes <i> here'
        }
config['Network']['interfaces'] = {
        'type':'file',
        'filename':'/etc/network/interfaces',
        'title':'interfaces file',
        'description':'configuration of network'
        }

config["Graphics"]={
        'xorg.conf':{
            'type':'file',
            'filename':'/etc/X11/xorg.conf',
            'title':'Xorg configuration file',
            'description':'detailed config of your graphics config file'
            }
        }
config.write()
"""

def validate_diag(diag):
    # checks for type, filename, title description and command
    if not 'type' in diag.keys(): # exists
        # invalid
        return False
    if not 'title' in diag.keys():
        return False
    if not 'description' in diag.keys():
        return False

    if diag['type'] == 'file':
        if not diag['filename']:
            return False
    elif diag['type'] == 'command':
        if not diag["command"]:
            return False
    else:
        return False

    return True





def get_all_tests(filename = 'diagtest.list'):
    # These are the categories as defined by the config file.
    config = ConfigObj(filename)
    # they need to be converted into regular category files
    #print config
    categories = {} 
    for category1 in config:# UGLY UGLY HACK
        #log.info("Processing a category")
        diag_category = {} # contains all the tests in this category
        category = config[category1]
        for test1 in category: #UGLY UGLY HACK
            test =  category[test1]
            if not validate_diag(test):
                continue #skip the not validated entry
            diag = Diagnostic()
            if test["type"] == "file":
                # Handle file based type
                diag.new_file_diagnosis(test["filename"],
                        test["title"],
                        test["description"])
            elif test["type"] == "command":
                # Handle a command
                diag.new_exec_diagnosis(test["command"],
                        test["title"],
                        test["description"])
                # now add the diagnosis to the category
            diag_category[test1] = diag

        # add the category to the list of categories
        categories[category1] = diag_category
    return categories

